﻿using System;

using BrnShop.Core;

namespace BrnShop.ConfigStrategy.File
{
    /// <summary>
    /// 基于文件的配置策略
    /// </summary>
    public partial class ConfigStrategy
    {
        #region 私有字段

        private readonly string _rdbsconfigfilepath = "Configs/rdbs.config";//关系数据库配置信息文件路径
        private readonly string _shopconfigfilepath = "Configs/shop.config"; 
        private readonly string _NCResponse = "Configs/NCResponse.xml"; 
        #endregion

        #region 帮助方法

        /// <summary>
        /// 从文件中加载配置信息
        /// </summary>
        /// <param name="configInfoType">配置信息类型</param>
        /// <param name="configInfoFile">配置信息文件路径</param>
        /// <returns>配置信息</returns>
        private IConfigInfo LoadConfigInfo(Type configInfoType, string configInfoFile)
        {
            return (IConfigInfo)IOHelper.DeserializeFromXML(configInfoType, configInfoFile);
        }

        /// <summary>
        /// 将配置信息保存到文件中
        /// </summary>
        /// <param name="configInfo">配置信息</param>
        /// <param name="configInfoFile">保存路径</param>
        /// <returns>是否保存成功</returns>
        private bool SaveConfigInfo(IConfigInfo configInfo, string configInfoFile)
        {
            return IOHelper.SerializeToXml(configInfo, configInfoFile);
        }

        #endregion

        /// <summary>
        /// 获得关系数据库配置
        /// </summary>
        public RDBSConfigInfo GetRDBSConfig()
        {
            return (RDBSConfigInfo)LoadConfigInfo(typeof(RDBSConfigInfo), IOHelper.GetMapPath(_rdbsconfigfilepath));
        }

        public ufinterface GetNCResponse()
        {
            return (ufinterface)LoadConfigInfo(typeof(sendresult), IOHelper.GetMapPath(_NCResponse));
        }

        public bool SaveNCResponse(ufinterface configInfo)
        {
            return SaveConfigInfo(configInfo, IOHelper.GetMapPath(_NCResponse));
        }


        /// <summary>
        /// 保存商城基本配置
        /// </summary>
        /// <param name="configInfo">商城基本配置信息</param>
        /// <returns>是否保存结果</returns>
        public bool SaveShopConfig(ShopConfigInfo configInfo)
        {
            return SaveConfigInfo(configInfo, IOHelper.GetMapPath(_shopconfigfilepath));
        }

        /// <summary>
        /// 获得商城基本配置
        /// </summary>
        public ShopConfigInfo GetShopConfig()
        {
            return (ShopConfigInfo)LoadConfigInfo(typeof(ShopConfigInfo), IOHelper.GetMapPath(_shopconfigfilepath));
        }
    }
}
